#include "stdafx.h"
#include "graphics/OpenGLWnd.h"
#include "graphics/Texture.h"	// Added by ClassView
#include "graphics/GrCamera.h"
#include "graphics/GrVector.h"
#include "ChildView.h"

bool LilliputFlag;

// IF YOU UPDATE WALL COLORS, UPDATE IT HERE TOO...OR TELL ANDY..
#define BIGRIGS_MESSUP() { { if (bigrigs &&bigrigs_messer_upper_interval > 0 &&bigrigs_messer_upper_interval < 900 * rand() / (double)RAND_MAX){ glColor3d(0,0,0); } else { glColor3d(1,1,1);} } }

inline void Quad(GLdouble *v1, GLdouble *v2, GLdouble *v3, GLdouble *v4)
{
	//CTexture tex;

	//tex.LoadFile("metal.bmp");

	//glEnable(GL_TEXTURE_2D);
	//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	//glBindTexture(GL_TEXTURE_2D, tex.TexName());

    glBegin(GL_QUADS);
    glVertex3dv(v1);
	//glTexCoord2f(0, 0);

    glVertex3dv(v2);
	//glTexCoord2f(1, 0);

    glVertex3dv(v3);
	//glTexCoord2f(1, 1);

    glVertex3dv(v4);
	//glTexCoord2f(0, 1);

    glEnd();
}

void Box(GLdouble p_x, GLdouble p_y, GLdouble p_z, const GLdouble *p_color)
{
    GLdouble a[] = {0., 0., p_z};
    GLdouble b[] = {p_x, 0., p_z};
    GLdouble c[] = {p_x, p_y, p_z};
    GLdouble d[] = {0., p_y, p_z};
    GLdouble e[] = {0., 0., 0.};
    GLdouble f[] = {p_x, 0., 0.};
    GLdouble g[] = {p_x, p_y, 0.};
    GLdouble h[] = {0., p_y, 0.};

    // I'm going to mess with the colors a bit so 
    // the faces will be visible in solid shading

    //glColor3d(p_color[0], p_color[1], p_color[2]);
    glNormal3d(0, 0, 1);
    Quad(a, b, c, d); // Front

    //glColor3d(p_color[0] * 0.95, p_color[1] * 0.95, p_color[2] * 0.95);
	glNormal3d(1, 0, 0);
    Quad(c, b, f, g); // Right

    //glColor3d(p_color[0] * 0.85, p_color[1] * 0.85, p_color[2] * 0.85);
	glNormal3d(0, 0, -1);
    Quad(h, g, f, e); // Back

    //glColor3d(p_color[0] * 0.90, p_color[1] * 0.90, p_color[2] * 0.90);
	glNormal3d(-1, 0, 0);
    Quad(d, h, e, a); // Left

    //glColor3d(p_color[0] * 0.92, p_color[1] * 0.92, p_color[2] * 0.92);
	glNormal3d(0, 1, 0);
    Quad(d, c, g, h); // Top

    //glColor3d(p_color[0] * 0.80, p_color[1] * 0.80, p_color[2] * 0.80);
	glNormal3d(0, -1, 0);
    Quad(e, f, b, a); // Bottom
}
void Wall(double L, double x, double z, double Rot)
{
	double W = 1.;
	double H = 3.;

	if(LilliputFlag == 1){
		//Apply temp scale
		x /= 10;
		z /= 10;
		L /= 10;
		W /= 10;
		H /= 10;
	}

	const double RED[] = {0.7, 0.0, 0.0};
	glPushMatrix();
	glTranslated(x,-2.0,z);
	glRotated(Rot,0.,1.,0.);
	Box(L, H, W, RED);
	glPopMatrix();
}
void Zach(DWORD elasped, DWORD actual, bool bigrigs)
{
	glEnable(GL_AMBIENT);
	glEnable(GL_DIFFUSE);
	//m_texture.LoadFile("dirt.bmp");

	GLfloat blue[] = {.12f, .56f, 1.f, 1.f};
    GLfloat msugreen[] = {0.f, .47f, .2f, 1.f};
    glMaterialfv(GL_FRONT, GL_DIFFUSE, blue);

	//Make everything micro-size for bird's eye view
	LilliputFlag = 0;

	const DWORD bigrigs_messer_upper_interval = actual % 1000;

	// add BIGRIGS_MESSUP(); before a wall you might want to mess up
	// I think I already added enough places
	// - Andy

	/*if (bigrigs &&
		bigrigs_messer_upper_interval > 100 &&
		bigrigs_messer_upper_interval < 300)
	{
		// bigrigs mode: mess stuff up
		// Warning: epilepsy possible
		// err towards red?
		//glColor3d(0.6 * rand() / (double)RAND_MAX, 0.3 * rand() / (double)RAND_MAX, 0.3 * rand() / (double)RAND_MAX);
	}
	else if (bigrigs &&
		bigrigs_messer_upper_interval > 0 &&
		bigrigs_messer_upper_interval < 900 * rand() / (double)RAND_MAX)
	{
		glColor3d(0,0,0); // kinda hide the wall
	}
	else
	{
		glColor3d(1,1,1); // reset
	}*/

	//Outer boundary of maze
	BIGRIGS_MESSUP();
	Wall(49., 1., 0.,270.);
	Wall(37., 1., 0.,  0.);
	BIGRIGS_MESSUP();
	Wall(49.,38., 1.,270.);
	Wall(37.,37.,50.,180.);

	//Entrance to maze
	Wall( 2., 1.,17.,  0.);
	Wall( 5., 3.,18.,270.);
	Wall( 4., 3.,24.,270.);
	Wall( 2., 1.,28.,  0.);

	//Top part of maze approx in region Z=0 to Z=15
	Wall(19., 1., 3.,  0.);
	Wall( 5.,24., 1.,270.);
	Wall(22., 5., 6.,  0.);
	BIGRIGS_MESSUP();
	Wall( 5.,28., 6.,270.);
	Wall( 5.,26., 3.,  0.);
	Wall( 7.,31., 4.,270.);
	Wall( 8.,35., 1.,270.);

	Wall( 7.,30.,11.,  0.);
	Wall( 2.,24., 7.,270.);
	Wall( 8., 5., 6.,270.);
	BIGRIGS_MESSUP();
	Wall( 2., 4.,14.,  0.);
	Wall( 3., 6.,15.,270.);
	BIGRIGS_MESSUP();
	Wall( 2.,21., 9.,270.);

	//Walls in classroom vicinity
	Wall( 6., 9., 9.,270.);
	Wall( 8., 9., 9.,  0.);
	Wall( 6.,18., 9.,270.);
	BIGRIGS_MESSUP();
	Wall( 2., 8.,15.,  0.);
	Wall(12.,11.,15.,  0.);
	Wall( 5.,20.,11.,  0.);
    Wall( 2.,24.,12.,  0.);
    Wall( 2.,26.,13.,270.);
    Wall( 9.,25.,15.,  0.);

    //Middle region of maze approx Z=15 to Z=30
    Wall( 4., 5.,18.,  0.);
    Wall( 3., 7.,19.,270.);
    Wall( 7., 7.,21.,  0.);
	Wall( 6.,12.,18.,  0.);
	Wall( 7.,18.,19.,270.);
	Wall( 7.,18.,25.,  0.);
	Wall( 5.,22.,18.,270.);
	Wall( 6.,22.,22.,  0.);
	Wall(12.,25.,18.,  0.);
	Wall( 6.,31.,19.,270.);
	Wall( 6.,28.,23.,270.);
	Wall( 5.,28.,28.,  0.);
	Wall( 7.,34.,22.,270.);
	Wall( 4.,23.,28.,  0.);
	Wall( 9., 7.,24.,270.);
	Wall( 7., 7.,24.,  0.);
	Wall( 3., 3.,32.,  0.);
	Wall( 3., 7.,32.,  0.);
	Wall( 5.,10.,27.,270.);
	Wall(11.,14.,25.,270.);
	Wall( 2.,24.,29.,270.);

	//Bottom region of maze approx Z=30 to Z=50
	Wall(13., 1.,36.,  0.);	
	Wall( 5.,20.,31.,270.);	
	Wall( 4.,22.,33.,  0.);	
	Wall( 9.,28.,31.,  0.);	
	Wall( 6.,17.,36.,  0.);	
	Wall( 4.,19.,30.,  0.);
	Wall(15.,27.,34.,270.);	
	Wall( 2.,26.,36.,270.);	
	Wall( 8.,27.,34.,  0.);	
	Wall( 4.,27.,37.,  0.);	
	Wall( 4.,33.,37.,  0.);	
	Wall( 7.,23.,37.,270.);	
	Wall(14., 5.,39.,  0.);	
	Wall( 7., 5.,39.,270.);	
	Wall(15., 7.,43.,  0.);	
	Wall( 3.,24.,43.,270.);	
    Wall(20., 4.,46.,  0.);

	//Need to still texturize all wall sections either shrub or brick. The current breakup of 
	//maze regions (top, middle, bottom) may mix between inside & outside.

	//Still need moving gnomes / wall sections to avoid during gameplay

	//Need to implement collision detection

	//Import dragon for end battle
	
	//Projector screen, podium, seats, inside classroom walls, ceiling for inside 472 classroom in maze
};